package ui.media 
{
	import com.gaiaframework.api.Gaia;
	import com.gaiaframework.assets.BitmapSpriteAsset;
	import com.greensock.TweenMax;
	import com.lextalkington.parallax.ParallaxEngine;
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.external.ExternalInterface;
	import flash.geom.Rectangle;
	import model.Model;
	import pages.Pages;
	import uk.soulwire.utils.display.Alignment;
	import uk.soulwire.utils.display.DisplayUtils;
	
	/**
	 * ...
	 * @author usual
	 */
	public class DiaporamaItem extends MovieClip 
	{
		private var _xmlData:XML;
		private var _displayObjectAsset:DisplayObject;
		private var _pE:ParallaxEngine;
		private var _bitmapSpriteAsset:BitmapSpriteAsset;
		private var _sens:String;
		public var itemWidth:int = 300;
		public var itemHeight:int = 300;
		private var _description:DiaporamaDescription;
		private var _assetsPage:String;
		public function DiaporamaItem(xmlData:XML,sens:String='h',assetsPage:String=Pages.PHOTOS) 
		{
			_assetsPage = assetsPage;
			_xmlData = xmlData;
			_sens = sens;
			addEventListener(Event.ADDED_TO_STAGE, _build);
		}
		
		private function _build(e:Event):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, _build);
			reparentAsset();
			addDescription();
			buttonMode = true;
			addEventListener(MouseEvent.CLICK, _onClick);
			addEventListener(MouseEvent.MOUSE_OVER, _onOver);
			addEventListener(MouseEvent.MOUSE_OUT, _onOut);
			
		}
		
		private function addDescription():void 
		{
			_description = new DiaporamaDescription(_xmlData.@id);
			addChild(_description);
			TweenMax.to(_description, 0, { autoAlpha:0 } );
		}
		
		private function _onOut(e:MouseEvent):void 
		{
			TweenMax.to(_pE, 1, { removeTint:true } );
			TweenMax.to(_description, 1, { autoAlpha:0 } );
		}
		
		private function _onOver(e:MouseEvent):void 
		{
			TweenMax.to(_pE, 1, { colorTransform: { tint:0x000000, tintAmount:0.5 }} );
			TweenMax.to(_description, 1, { autoAlpha:1 } );

		}
		
		private function _onClick(e:MouseEvent):void 
		{
			if(_sens=='h')
			{
				if (ExternalInterface.available) ExternalInterface.call("showProduct", String(_xmlData.@id), String(Gaia.api.getPage(Pages.PHOTOS).assets[_xmlData.@id + '-xml'].xml));
			}
			if (_sens == 'v')
			{
				Gaia.api.goto(Pages.TIMELINE + '/' + String(_xmlData.@id));
			}
		}
		
		private function reparentAsset():void 
		{
			_bitmapSpriteAsset = Gaia.api.getPage(_assetsPage).assets[_xmlData.@id]
			_bitmapSpriteAsset.smoothing = _bitmapSpriteAsset.visible = true;
			_displayObjectAsset = _bitmapSpriteAsset.container;
			var sh:int = itemHeight;
			var sw:int = itemWidth;
			if(_sens=='h')_pE = new ParallaxEngine(sw,stage.stageHeight,5, ParallaxEngine.X_AXIS);
			if(_sens=='v')_pE = new ParallaxEngine(stage.stageWidth,sh, 5, ParallaxEngine.Y_AXIS);
			_pE.addPlate(_displayObjectAsset);
			addChild(_pE);
			_pE.start();
		}
		
		public function resize():void 
		{
			_pE.pause();
			var sh:int = stage.stageHeight;
			var sw:int = stage.stageWidth;
			if (_sens == 'h')
			{
				var area:Rectangle = new Rectangle( 0, 0, _displayObjectAsset.width, sh );
				DisplayUtils.fitIntoRect( _displayObjectAsset, area, true, Alignment.BOTTOM );
				_pE._height = itemHeight;
				_pE._mask.height = sh;
				_description.rotation = -90;
				_description.y = stage.stageHeight-200;
				_description.x = 20;
			}
			if (_sens == 'v')
			{
				var area2:Rectangle = new Rectangle(0, 0, sw, _displayObjectAsset.height);
				DisplayUtils.fitIntoRect( _displayObjectAsset, area2, true, Alignment.BOTTOM );
				_pE._height = sh;
				_pE._mask.width = sw;
				_description.x = 20;
				_description.y = itemHeight-(_description.height+20);
			}
			_pE.start();
		}		
		
		
		
	}

}